package org.jeecg.modules.ywMaterialInventory.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.constant.CommonConstant;
import org.jeecg.constant.InventoryConstant;
import org.jeecg.modules.ywMaterialInventory.entity.YwMaterialInventoryInfo;
import org.jeecg.modules.ywMaterialInventory.mapper.YwMaterialInventoryInfoMapper;
import org.jeecg.modules.ywMaterialInventory.service.IYwMaterialInventoryInfoService;
import org.jeecg.modules.ywMaterialOut.entity.YwMaterialOutDetail;
import org.jeecg.modules.ywMaterialOut.entity.YwMaterialOutMain;
import org.jeecg.modules.ywMaterialOut.service.IYwMaterialOutDetailService;
import org.jeecg.modules.ywMaterialOut.service.IYwMaterialOutMainService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 材料库存
 * @Author: jeecg-boot
 * @Date: 2024-08-22
 * @Version: V1.0
 */
@Service
public class YwMaterialInventoryInfoServiceImpl extends ServiceImpl<YwMaterialInventoryInfoMapper, YwMaterialInventoryInfo> implements IYwMaterialInventoryInfoService {

    @Resource
    private YwMaterialInventoryInfoMapper ywMaterialInventoryInfoMapper;
    @Resource
    private IYwMaterialOutMainService ywMaterialOutMainService;
    @Resource
    private IYwMaterialOutDetailService ywMaterialOutDetailService;

    @Override
    public List<YwMaterialInventoryInfo> selectList(String productCode, String productName) {
        List<YwMaterialInventoryInfo> list = ywMaterialInventoryInfoMapper.getList(productCode, productName);
        return list;
    }

    @Override
    public void mobile(String inventoryId, BigDecimal inventoryWeight, String goodsPosition) {
        // 想要移库的那条库存
        YwMaterialInventoryInfo inventoryInfo = this.ywMaterialInventoryInfoMapper.selectById(inventoryId);
        Assert.notNull(inventoryInfo, "不存在的库存信息,请联系管理员！");
        Assert.isTrue(inventoryInfo.getStatus().equals(CommonConstant.StatusEnum.YES.getCode()), "库存信息已被删除,请刷新后重试！");

        Assert.isTrue(inventoryWeight.compareTo(inventoryInfo.getInventoryWeight()) <= 0, "移库的重量不能大于库存的重量！");

        // 新增移库的库存
        YwMaterialInventoryInfo inventory = new YwMaterialInventoryInfo();
        inventory.setHistoryId(inventoryInfo.getId());
        inventory.setEnterId(inventoryInfo.getEnterId());
        inventory.setEnterDate(inventoryInfo.getEnterDate());
        inventory.setBillNo(inventoryInfo.getBillNo());
        inventory.setProductCode(inventoryInfo.getProductCode());
        inventory.setProductName(inventoryInfo.getProductName());
        inventory.setGoodsPosition(goodsPosition);
        inventory.setManufactureDate(inventoryInfo.getManufactureDate());
        inventory.setStatus(CommonConstant.StatusEnum.YES.getCode());

        inventory.setInventoryWeight(inventoryWeight);
        inventory.setInventoryVolume(inventoryWeight.multiply(new BigDecimal("1.4")).multiply(new BigDecimal("0.001")));
        this.ywMaterialInventoryInfoMapper.insert(inventory);

        // 更新现有库存
        inventoryInfo.setInventoryWeight(inventoryInfo.getInventoryWeight().subtract(inventoryWeight));
        inventoryInfo.setInventoryVolume(inventoryInfo.getInventoryWeight().multiply(new BigDecimal("1.4")).multiply(new BigDecimal("0.001")));
        this.ywMaterialInventoryInfoMapper.updateById(inventoryInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outBatch(List<String> wantOutIdList, Date outDate, String billNo) {
        List<YwMaterialInventoryInfo> inventoryInfoList = this.ywMaterialInventoryInfoMapper.selectBatchIds(wantOutIdList);
        Assert.isTrue(inventoryInfoList.size() == wantOutIdList.size(), "出库失败,请检查数据是否异常！");

        YwMaterialOutMain outMain = new YwMaterialOutMain();
        outMain.setOutDate(outDate);
        outMain.setOutStatus(InventoryConstant.OutStatusEnum.PREPARE_OUT.getCode());
        ywMaterialOutMainService.save(outMain);

        List<YwMaterialOutDetail> outDetailList = inventoryInfoList.stream().map(data -> {
            YwMaterialOutDetail outDetail = new YwMaterialOutDetail();
            outDetail.setOutId(outMain.getId());
            outDetail.setBillNo(billNo);
            outDetail.setProductCode(data.getProductCode());
            outDetail.setProductName(data.getProductName());
            outDetail.setOutWeight(data.getInventoryWeight());
            outDetail.setInventoryId(data.getId());
            outDetail.setEnterDate(data.getEnterDate());
            outDetail.setGoodsPosition(data.getGoodsPosition());
            outDetail.setInventoryWeight(data.getInventoryWeight());
            outDetail.setOutVolume(data.getInventoryVolume());
            outDetail.setStatus(CommonConstant.StatusEnum.YES.getCode());

            data.setInventoryWeight(BigDecimal.ZERO);
            return outDetail;
        }).collect(Collectors.toList());
        ywMaterialOutDetailService.saveBatch(outDetailList);

        this.updateBatchById(inventoryInfoList);
    }

}
